package com.ctshk.rpc.order.season.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.OrderPaymentStatus;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.order.season.dto.SeasonOrderInsuranceDTO;
import com.ctshk.rpc.order.season.dto.SeasonOrderTouristDataDTO;
import com.ctshk.rpc.order.season.entity.SeasonOrder;
import com.ctshk.rpc.order.season.entity.SeasonOrderInsurance;
import com.ctshk.rpc.order.season.entity.SeasonOrderTourist;
import com.ctshk.rpc.order.season.mapper.SeasonOrderInsuranceMapper;
import com.ctshk.rpc.order.season.mapper.SeasonOrderMapper;
import com.ctshk.rpc.order.season.mapper.SeasonOrderTouristMapper;
import com.ctshk.rpc.order.season.req.SeasonOrderTouristDataReq;
import com.ctshk.rpc.order.season.service.ISeasonOrderTouristService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;


/**
 * <p>
 * 套票订单旅客信息 服务实现类
 * </p>
 *
 * @author 杨林
 * @since 2021-03-24
 */
@DubboService
public class SeasonOrderTouristServiceImpl extends ServiceImpl<SeasonOrderTouristMapper, SeasonOrderTourist> implements ISeasonOrderTouristService {

    @Autowired
    private SeasonOrderTouristMapper touristMapper;

    @Autowired
    private SeasonOrderInsuranceMapper insuranceMapper;

    @Autowired
    private SeasonOrderMapper orderMapper;

    /* @MethodName: 获取旅客类型 根据订单id
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/29 17:48
     **/
    @Override
    public List<SeasonOrderTouristDataDTO> getTouristListById(Long id) {
        List<SeasonOrderTouristDataDTO> resultList = new ArrayList();
        List<SeasonOrderTourist> list = touristMapper.selectList(
                new QueryWrapper<SeasonOrderTourist>().lambda()
                        .eq(SeasonOrderTourist::getIsDeleted, IsDeletedCode.NO.getCode())
                        .eq(SeasonOrderTourist::getOrderId, id)

        );

        for (SeasonOrderTourist tourist : list) {
            SeasonOrderTouristDataDTO dto = EntityUtil.copy(tourist, SeasonOrderTouristDataDTO.class);
            String touristId = String.valueOf(tourist.getId());
            Long orderId = tourist.getOrderId();
            SeasonOrderInsuranceDTO insuranceDTO = null;
            SeasonOrderInsurance insurance = getInsuranceInfo(orderId, touristId);
            if (insurance != null) {
                insuranceDTO = EntityUtil.copy(insurance, SeasonOrderInsuranceDTO.class);
            }
            dto.setOrderInsuranceDTO(insuranceDTO);
            resultList.add(dto);

        }
        return resultList;
    }

    @Override
    public Result editTourist(TokenUser user, SeasonOrderTouristDataReq req) {
        SeasonOrderTourist tourist = touristMapper.selectById(req.getId());
        if (Objects.isNull(tourist)) {
            return Result.failed(SystemError.SYS_411);
        }
        SeasonOrder order = orderMapper.selectById(tourist.getOrderId());
        if (Objects.isNull(order)) {
            return Result.failed(SystemError.SEASON_TICKET_20017);
        }
        //
        Integer paymentStatus = order.getPaymentStatus();
        //不是待付款 的 不能修改的
        if (OrderPaymentStatus.UN_PAYMENT.getCode() != paymentStatus) {
            return Result.failed(SystemError.SEASON_TICKET_20031);
        }
        tourist = EntityUtil.copy(req, SeasonOrderTourist.class);
        touristMapper.updateById(tourist);
        return Result.success();
    }

    /* @MethodName: 编辑获取旅客信息
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/31 17:44
     **/
    @Override
    public Result findTouristInfo(TokenUser user, Long id) {
        SeasonOrderTourist tourist = touristMapper.selectById(id);
        if (Objects.isNull(tourist)) {
            return Result.failed(SystemError.SYS_411);
        }
        SeasonOrder order = orderMapper.selectById(tourist.getOrderId());
        if (Objects.isNull(order)) {
            return Result.failed(SystemError.SEASON_TICKET_20017);
        }
        //
        Integer paymentStatus = order.getPaymentStatus();
        //不是待付款 的 不能修改的
        if (OrderPaymentStatus.UN_PAYMENT.getCode() != paymentStatus) {
            return Result.failed(SystemError.SEASON_TICKET_20031);
        }
        SeasonOrderTouristDataDTO dto = EntityUtil.copy(tourist, SeasonOrderTouristDataDTO.class);
        return Result.success(dto);
    }


    /* @MethodName: 获取保险信息
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/3/29 19:58
     **/
    public SeasonOrderInsurance getInsuranceInfo(Long orderId, String touristId) {
        SeasonOrderInsurance insurance = insuranceMapper.selectOne(
                new QueryWrapper<SeasonOrderInsurance>().lambda()
                        .eq(SeasonOrderInsurance::getIsDeleted, IsDeletedCode.NO.getCode())
                        .eq(SeasonOrderInsurance::getOrderId, orderId)
        );
        if (insurance != null) {
            String insuredPerson = insurance.getInsuredPerson();
            if (!insuredPerson.contains(",")) {
                if (touristId.equals(insuredPerson)) {
                    return insurance;
                }
            } else {
                List<String> strList = Arrays.asList(insuredPerson.split(","));
                if (strList.contains(touristId)) {
                    return insurance;
                }
            }

        }

        return null;
    }

    /* @MethodName: 获取旅客信息
     * @Description: TODO
     * @Param:
     * @Return:
     * @Author: 杨林
     * @Date:2021/4/7 15:20
     **/
    public List<SeasonOrderTouristDataDTO> getTouristList(Long id) {
        List<SeasonOrderTourist> list = touristMapper.selectList(
                new QueryWrapper<SeasonOrderTourist>().lambda()
                        .eq(SeasonOrderTourist::getIsDeleted, IsDeletedCode.NO.getCode())
                        .eq(SeasonOrderTourist::getOrderId, id)

        );
        return EntityUtil.copyList(list, SeasonOrderTouristDataDTO.class);

    }

    @Override
    public List<SeasonOrderInsuranceDTO> getOrderInsurance(Long orderId) {
        List<SeasonOrderInsurance> insurances = insuranceMapper.selectList(
                new QueryWrapper<SeasonOrderInsurance>().lambda()
                        .eq(SeasonOrderInsurance::getIsDeleted, IsDeletedCode.NO.getCode())
                        .eq(SeasonOrderInsurance::getOrderId, orderId)
        );
        return EntityUtil.copyList(insurances, SeasonOrderInsuranceDTO.class);
    }
}
